package elEMAS.impl;

import java.util.Properties;

import common.CommonHelper;
import common.GeneticHelper;
import common.GeneticOperations;

public abstract class EmasRunner<Genotype> {
    
    public abstract GeneticOperations<Genotype> getGeneticOperations(Properties properties, EmasParameters params);
    
    public String getPrefix(String inputFile) {
        
        String prefix = CommonHelper.fromInputfilenameToPrefix(inputFile);
        prefix = "output/"+prefix+"_emas_";
        return prefix;
    }
    
    public void start(String problemName, String[] args) {
              
        CommonHelper.printAssertionState();
        System.out.println("EMAS "+problemName+" Witaj !!!");
        System.out.println();
        
        if (args.length != 1) {
            System.out.println("Parametry: [plik_properties]");
            System.exit(1);
        }
        
        Properties properties = CommonHelper.readProperties(args[0]);        
        EmasParameters params = ElEmasHelpers.getEmasParameters(properties);
        params.configFile = args[0];

        GeneticOperations<Genotype> go = getGeneticOperations(properties, params);
        EmasAlgorithm<Genotype> er = new EmasAlgorithm<Genotype>(go,params);
        er.printParameters();
        CommonHelper.sleep(1000);
        
        // ustalanie nazw plików
        String prefix = getPrefix(params.inputFile);
        
        CommonHelper.printToFile(prefix+"random.txt", GeneticHelper.populationToString(er.getAllAgents()));
        
        er.run();
        er.printResultStatistics();
        
        
        String HvInTime = CommonHelper.HvInTimeToStr(er.getHvInTime());
        CommonHelper.printToFile(prefix+"hv.txt", HvInTime);
        
        GeneticHelper.savePopulationToFile(prefix+"nd",er.getAllNonDominatedAgents());
        GeneticHelper.savePopulationToFile(prefix+"all",er.getAllAgents());
        
        //GeneticHelper.updateBest(go, bestFile, bestGenotypesFile, er.getElitists(), params.ref_f0, params.ref_f1);
        
    }
}
